package com.bwie.service.impl;

import com.bwie.exception.BusinessException;
import com.bwie.mapper.RechargeMapper;
import com.bwie.mapper.SmsMapper;
import com.bwie.pojo.Result;
import com.bwie.pojo.dto.RechargeDto;
import com.bwie.pojo.po.Recharge;
import com.bwie.pojo.po.Sms;
import com.bwie.pojo.po.User;
import com.bwie.pojo.vo.RechargeVo;
import com.bwie.pojo.vo.UserVo;
import com.bwie.service.RechargeService;
import com.bwie.utils.AesUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RechargeServiceImpl implements RechargeService {
    @Autowired
    private RechargeMapper rechargeMapper;

    @Autowired
    private HttpSession session;
    @Autowired
    private SmsMapper smsMapper;
    @Override
    public PageInfo<Recharge> getRechargeList(RechargeDto rechargeDto) {
        User user = (User) session.getAttribute("user");
        PageHelper.startPage(rechargeDto.getPageNum(),rechargeDto.getPageSize());
        List<Recharge> list = rechargeMapper.getRechargeList(rechargeDto);
        PageInfo<Recharge> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Transactional
    @Override
    public Result addRechsrge(Recharge recharge) throws Exception {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
        recharge.setUserId(userId);
        recharge.setRechargeTime(new Date());
        recharge.setRechargeState(0);
        BigDecimal rechargeMoney = recharge.getRechargeMoney();
        int i = rechargeMoney.compareTo(new BigDecimal(0));
        if (i<=0){
            return Result.error("充值金额必须大于0元");
        }
        Integer parkId = recharge.getParkId();
        if (null==rechargeMoney||null==parkId){
            return Result.error("必要信息不能为空");
        }
        recharge.setRechargeKouling(recharge.getRechargeMoney()+"_"+recharge.getUserId());
        String rechargeKouling = recharge.getRechargeKouling();
        String encrypt = AesUtil.encrypt(rechargeKouling);
        recharge.setRechargeKouling(encrypt);
        try {
            int reshargeId1 =rechargeMapper.addRechsrge(recharge);


            Sms sms = new Sms();
            sms.setSmsRemarks("充值短信");
            sms.setSmsTime(new Date());
            sms.setRechargeId(reshargeId1);
            sms.setRechargeKouling(encrypt);

            smsMapper.addSms(sms);
            return Result.success();
        } catch (Exception e) {
            return Result.error("充值异常");
        }

    }

    @Override
    public PageInfo<RechargeVo> getRecharge(RechargeDto rechargeDto) {

        PageHelper.startPage(rechargeDto.getPageNum(),rechargeDto.getPageSize());
        List<RechargeVo> rechargeVoList = new ArrayList<>();
        User user = (User) session.getAttribute("user");
        Integer roleId = rechargeMapper.getRole(user.getUserId());
        System.out.println(roleId);
        if (roleId==1){
            rechargeVoList = rechargeMapper.getRecharge(null,rechargeDto);
        }
        if (roleId==2){
            rechargeVoList = rechargeMapper.getRecharge(user.getUserId(),rechargeDto);
        }
        PageInfo<RechargeVo> pageInfo = new PageInfo<>(rechargeVoList);
        return pageInfo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addMoney(RechargeVo rechargeVo) {
        Integer rechargeState = rechargeVo.getRechargeState();
        if (rechargeState==1){
            throw new BusinessException("已充值过无法充值");
        }
        Integer userId = rechargeVo.getUserId();
        BigDecimal rechargeMoney = rechargeVo.getRechargeMoney();
        rechargeMapper.updMoney(userId,rechargeMoney);
        Integer rechargeId = rechargeVo.getRechargeId();
        rechargeMapper.updState(rechargeId);
        UserVo userVo = (UserVo) session.getAttribute("user");
        UserVo userVo1 = new UserVo();
        BeanUtils.copyProperties(userVo,userVo1);
        userVo1.setUserMoney(userVo.getUserMoney().add(rechargeMoney));
        session.setAttribute("user",userVo1);
    }

    @Override
    public void addRechsrgeList(List<Recharge> list) {
        rechargeMapper.addRechsrgeList(list);
    }
}
